mapResult

inline fun <T, R> Result<T>.mapResult(transform: (T) -> Result<R>): Result<R>(source)

If the result is successful, replaces it with the value returned by the transform operation.

Samples

import dev.kikugie.commons.result.*
import kotlin.test.assertIs
import kotlin.test.assertTrue

fun main() { 
   //sampleStart 
   fun example(fail: Boolean): Result<String> = runCatching {
    if (fail) throw IllegalStateException("Something went wrong")
    "Hello world!"
}

val failed = runCatching { example(false) }.mapResult { example(true) }
assertIs<IllegalStateException>(failed.exceptionOrNull())

val succeeded = runCatching { example(false) }.mapResult { example(false) }
assertIs<String>(succeeded.getOrNull()) 
   //sampleEnd
}